package cz.spock.cms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cz.spock.cms.entity.CSS;
import cz.spock.cms.entity.Node;
import cz.spock.cms.service.CSSService;
import cz.spock.cms.service.NodeService;
import cz.spock.cms.service.ProjectService;
import cz.spock.cms.service.TemplateSourceService;
import cz.spock.cms.template.entity.TemplateSource;
import cz.spock.core.controller.BaseController;

/**
 * interface between model and view
 * 
 * In model "page" is only abstract object represented by Node
 * Each Node with specified URL is supposed to be a page
 * 
 * TODO refactor, handle errors
 * 
 * @author miso
 *
 */
@Controller
public class PageController extends BaseController {

    @Autowired
    private NodeService nService;
    @Autowired
    private ProjectService pService;
    @Autowired
    private CSSService cService;
    @Autowired
    private TemplateSourceService tsService;
    
    // non-ajax
    
    @RequestMapping(value = "/cms/{project}/pages", method = RequestMethod.GET)
    public ModelAndView projectPages(@PathVariable Long project, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + project + "/pages");
        List<Node> nl = nService.findNodeByProject(pService.getProjectById(project));
        return new ModelAndView("cms/page/pages", "pages", nl).addObject("project", project);
    }

    @RequestMapping(value = "/cms/page/{id}", method = RequestMethod.GET)
    public ModelAndView page(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/page/" + id);
        Node n = nService.getNodeById(id);
        
        String data = nService.mapToHtml(nService.getDataTree(n), 1, new StringBuilder(100));
        
        return new ModelAndView("cms/page/page").addObject("page", n).addObject("data", data).addObject("css", n.getCss()).addObject("template", n.getTemplate());
    }

    @RequestMapping(value = "/cms/{project}/page/create", method = RequestMethod.GET)
    public ModelAndView pageCreateGet(@PathVariable String project, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + project + "/page/create get");
        List<CSS> styles = cService.getAllStyles();
        List<TemplateSource> templates = tsService.getAllTemplateSources();
        return new ModelAndView("cms/page/create").addObject("styles", styles).addObject("templates", templates).addObject( "project", project);
    }

    @RequestMapping(value = "/cms/{project}/page/create", method = RequestMethod.POST)
    public String pageCreatePost(@PathVariable Long project, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + project + "/node/create post");
        String style = request.getParameter("css");
        String template = request.getParameter("template");
        System.out.println("params taken");
        try {
            Node n = nService.createNode(request.getParameter("url"),
                    project.toString(),
                    request.getParameter("description"));
            System.out.println("node created");
            nService.setTemplate(Long.parseLong(template), n.getId());
            System.out.println("template set");
            if(style != null && style.equals("")) { 
                nService.setStyle(Long.parseLong(style), n);
            }
            System.out.println("style set");
        } catch (Exception e) {
            System.out.println(e.getClass().getName().toString());
            sendJson(response, pService.errorToJson(e.getMessage(), 0));
            return null;
        }
        return "redirect:/cms/" + project + "/pages";
    }

    @Deprecated
    // implementation later
    @RequestMapping(value = "/cms/page/delete", method = RequestMethod.POST)
    public String pageDelete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/page/delete");
        // delete project

        return "cms/projects";
    }

}
